const Controller = require('../controllers/Controller.js').controller;
const app = getApp();
Page({
  data: {
    globalStype:'',
    phoneNumber:'',
    openId:'',
    hasMobile: false, //没有手机号
    wxuserInfo:'',
    selected:false
  },

  onLoad(option) {
    this.setData({globalStype:app.globalData.globalStype})
    
  },

  //选择是否同意协议
  changeSelected(){
    console.log(123);
    if(this.data.selected==false){
      this.setData({
        selected:true
      })
    }else{
      this.setData({
        selected:false
      })
    }
  },

  goagree(){
    wx.navigateTo({
      url: "/pages/Login/agree?id=6"
    })
  },
  goPrivacy(){
    wx.navigateTo({
      url: "/pages/Login/agree?id=18"
    })
  },
  appletBdMobileLogin(){
    Controller.appletBdMobileLogin({
      mobile:this.data.phoneNumber,
      openId:app.globalData.openId,
      nick:this.data.wxuserInfo.nickName,
      head:this.data.wxuserInfo.avatarUrl
    }).then(result => {
      wx.setStorageSync('usertoken', result.data.sessionId)
      wx.showToast({
        title: '登录成功！',
        icon: 'success',
        duration: 2000
      })
      setTimeout(res => {
        wx.navigateBack({ //返回
          delta: 1
        })
      },1500)
    });
  },
  bindgetuserinfoBtn() {
    if(this.data.selected==false){
      wx.showToast({
        icon:"none",
        title: '请勾选相关协议',
      })
      return
    }

    console.log("触发")
    let that=this
    wx.getUserProfile({
      desc:"正在获取",
      success:function(res){
        if(res.userInfo){
          that.setData({
            hasMobile: true,
            wxuserInfo:res.userInfo
          })
          wx.setStorageSync('wxuserInfo', res.userInfo)
        }
      }
    })
  },
  // 手机授权
  bindgetphonenumber(e) {
    console.log(e)
    var phone=e.detail;
    wx.login({
      success: res => {
        if (res.code) {
          Controller.appletGetPhone({
            sessionKey: app.globalData.sessionKey,
            iv:phone.iv,
            encryptedData:phone.encryptedData
          }).then(result => {
              this.setData({
                phoneNumber:result.data.phoneNumber,
              })
              wx.setStorageSync('phonenumber',result.data.phoneNumber)
              this.appletBdMobileLogin()
          });
        } else {
          console.log('登录失败！' + res.errMsg)
        }
        
      }
    });

  },

  cancel(){
    wx.navigateBack({ //返回
      delta: 1
    })
  },

})